package com.ns.basic.server.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ns.basic.server.bo.PermissionBO;
import com.ns.basic.server.bo.ServerBO;
import com.ns.basic.server.bo.ServerPageBO;
import com.ns.basic.server.dto.ServerPageDto;
import com.ns.basic.server.dto.ServerPermissionDto;
import com.ns.basic.server.entry.ServerEntry;
import com.ns.basic.server.service.IServerMenuService;
import com.ns.basic.server.service.IServerService;
import com.ns.basic.server.vo.ServerEntryVO;
import com.ns.core.annotations.ReqLog;
import com.ns.core.bo.IdsBO;
import com.ns.core.current.config.SysConst;
import com.ns.core.utils.sg.ResultError;
import com.ns.core.utils.sg.ResultOk;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.BeanUtils;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import java.util.Objects;

/**
 * 服务WEB接口
 *
 * @author lw
 * @since 2024/09/11
 */
@RestController
@RequestMapping("/server/")
@RequiredArgsConstructor
@CrossOrigin
public class ServerEntryController {

    private final IServerService serverService;

    private final IServerMenuService menuService;


    /**
     * 增
     *
     * @param bo 参数
     */
    @PostMapping("add")
    @ReqLog(module = "应用模块", name = "增")
    @Transactional(rollbackFor = Exception.class)
    @CacheEvict(cacheNames = SysConst.PROJECT_TAG, key = "'" + SysConst.PROJECT_DICT + "'")
    public ResultOk add(@RequestBody ServerBO bo) {
        ServerEntry serverEntry = new ServerEntry();
        BeanUtils.copyProperties(bo, serverEntry);
        serverEntry.setSort(serverService.count() + 1);
        if (serverService.saveOrUpdate(serverEntry)) {
            return ResultOk.getSuccessData(serverEntry.getId());
        } else {
            throw new ResultError("添加失败");
        }
    }

    /**
     * 逻辑删
     *
     * @param id 参数
     */
    @GetMapping("delete")
    @ReqLog(module = "应用模块", name = "逻辑删")
    @CacheEvict(cacheNames = SysConst.PROJECT_TAG, key = "'" + SysConst.PROJECT_DICT + "'")
    public ResultOk delete(Long id) {
        return ResultOk.getByBoolean(serverService.removeById(id));
    }

    /**
     * 删 - 根据id列表
     */
    @PostMapping("deleteByIds")
    @ReqLog(module = "应用模块", name = "批量删")
    @CacheEvict(cacheNames = SysConst.PROJECT_TAG, key = "'" + SysConst.PROJECT_DICT + "'")
    public ResultOk deleteByIds(@RequestBody IdsBO bo) {
        return ResultOk.getByBoolean(serverService.removeBatchByIds(bo.getIds()));
    }

    /**
     * 改
     *
     * @param bo 参数
     */
    @PostMapping("update")
    @ReqLog(module = "应用模块", name = "改")
    @CacheEvict(cacheNames = SysConst.PROJECT_TAG, key = "'" + SysConst.PROJECT_DICT + "'")
    public ResultOk update(@RequestBody ServerBO bo) {
        ServerEntry serverEntry = new ServerEntry();
        BeanUtils.copyProperties(bo, serverEntry);
        return ResultOk.getByBoolean(serverService.updateById(serverEntry));
    }

    /**
     * 查
     *
     * @param id 参数
     */
    @GetMapping("getById")
    @ReqLog(module = "应用模块", name = "查")
    public ResultOk getById(Long id) {
        ServerEntry serverEntry = serverService.getById(id);
        ServerEntryVO vo = new ServerEntryVO();
        BeanUtils.copyProperties(serverEntry, vo);
        return ResultOk.getSuccessData(vo);
    }

    /**
     * 查 - 集合
     */
    @PostMapping("getList")
    @ReqLog(module = "应用模块", name = "集合")
    public ResultOk getList(@RequestBody ServerPageBO bo) {
        ServerPageDto dto = new ServerPageDto();
        BeanUtils.copyProperties(bo, dto);
        Page<ServerEntryVO> page = serverService.selectBy(dto);
        return ResultOk.getPageData(page.getTotal(), page.getRecords());
    }

    /**
     * 分配服务菜单
     *
     * @param bo 参数
     */
    @PostMapping("assist")
    @ReqLog(module = "应用模块", name = "分配权限")
    @CacheEvict(cacheNames = SysConst.SERVER_PERMISSION, key = "#bo.id")
    public ResultOk assist(@RequestBody PermissionBO bo) {
        try {
            if (Objects.isNull(bo.getPermissions())) {
                return ResultOk.getError("未知权限参数！");
            }
            if (Objects.isNull(bo.getId())) {
                return ResultOk.getError("未知服务参数！");
            }
            ServerPermissionDto dto = new ServerPermissionDto();
            BeanUtils.copyProperties(bo, dto);
            return ResultOk.getByBoolean(menuService.assistMenu(dto));
        } catch (Exception e) {
            return ResultOk.getError("租户权限分配失败！");
        }
    }
}
